﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using tju.cme.BookStore2024.webapi.Models.Tools;
using tju.cme.BookStore2024.webapi.Models.ViewModels.Message;
using tju.cme.BookStore2024.webapi.Services;

namespace tju.cme.BookStore2024.webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MessageController(MessageServices services):ControllerBase
    {
        /// <summary>
        /// 创建消息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("Create")]
        public async Task<IActionResult> Greate(MessageCreateModel model)
        {
            await services.GreateMessageAsync(model, GetAuthorizedInfo.GetId(User), GetAuthorizedInfo.GetRole(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="megId"></param>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("Send/{megId}")]
        public async Task<IActionResult> Send([FromRoute]Guid megId)
        {
            await services.SendeMessageAsync(megId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }



        /// <summary>
        /// 修改消息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("Modify")]
        public async Task<IActionResult> Modify(MessageModifyModel model)
        {
            await services.ModifyMessageAsync(model, GetAuthorizedInfo.GetId(User), GetAuthorizedInfo.GetRole(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 阅读消息
        /// </summary>
        /// <param name="megId"></param>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("Read/{megId}")]
        public async Task<IActionResult> Read(Guid megId)
        {
            await services.ReadMessageAsync(megId , GetAuthorizedInfo.GetId(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }



        /// <summary>
        /// 根据用户Id,返回所有消息
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("GetAllByUserId")]
        public async Task<IActionResult> GetAllByUserIds()
        {
            
            return Ok(new ReturnMessage(await services.GetAllByUserIdAsync(GetAuthorizedInfo.GetId(User))));
        }

        /// <summary>
        /// 根据用户Id,返回所有已接收到的消息
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("GetReceivedByUserId")]
        public async Task<IActionResult> GetReceivedByUserIds()
        {
            await services.GetReceivedByUserIdAsync(GetAuthorizedInfo.GetId(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 根据用户Id,返回所有已发送的消息
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("GetSentByUserId")]
        public async Task<IActionResult> GetSentByUserIds()
        {
            await services.GetSentByUserIdAsync(GetAuthorizedInfo.GetId(User)); 
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 根据用户Id,返回所有已读的消息
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Author,Admin")]
        [HttpPost("GetReadByUserId")]
        public async Task<IActionResult> GetReadByUserIds()
        {
            await services.GetReadByUserIdAsync(GetAuthorizedInfo.GetId(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }




    }
}
